(function ($) {

    var commonUtils = {};

    
    //获取当前时间
    commonUtils.todays = function show(){
   	    var mydate = new Date();
   	    var str = "" + mydate.getFullYear() + "-";
   	    str += (mydate.getMonth()+1) + "-";
   	    str += mydate.getDate() + "-";
   	    return str;
   }
    
    
  //进度条
    commonUtils.jinduTiao = function(value){
    	var taskTy = "<div class='progress'><div class='progress-bar progress-bar-no' style='width:25%'>25%</div>"+
    				"<div class='progress-bar progress-bar-success' style='width:20%'>20%</div>"+
    "<div class='progress-bar progress-bar-info' style='width:10%'>10%</div>"+
    "<div class='progress-bar progress-bar-warning' style='width:10%'>10%</div>"+
    "<div class='progress-bar progress-bar-danger' style='width:15%'>15%</div>"+
    "</div>";
    	return taskTy;
    }
    
    //格式化grid查看列
    commonUtils.typeformatter = function(action,views) {
        //var selectHtml="<a href='"+action+"'>"+views+"</a>";
    	var selectHtml="<button id='viewTaskBtn' class='btn_view btn-primary' type='button'><i class='fa fa-key'></i>查看 </button>";
        return selectHtml;
  	}
    
    
  //任务种类转化
    commonUtils.taskCla = function(value){
    	var taskClass = "";
    	if(value==null||value == ""||value==undefined){
            return "";
        }else{
        	switch(value)
        	{
        	case 1:
        		taskClass = "办公事宜";
        		break;
        	case 2:
        		taskClass = "文档编辑";
        		break;
        	case 3:
        		taskClass = "需求设计";
        		break;
        	case 4:
        		taskClass = "软件开发";
        		break;
        	case 5:
        		taskClass = "软件测试";
        		break;
        	default:
        		taskClass = "其它";
        	}
        }
    	return taskClass;
    }
    
    
  //任务种类转化
    commonUtils.taskType = function(value){
    	var taskTy = "";
    	if(value==null||value == ""||value==undefined){
            return "";
        }else{
        	switch(value)
        	{
	        	case 1:
	        		taskTy = "紧急重要";
	        		break;
	        	case 2:
	        		taskTy = "紧急不重要";
	        		break;
	        	case 3:
	        		taskTy = "不紧急重要";
	        		break;
	        	case 4:
	        		taskTy = "不紧急不重要";
	        		break;
	        	default:
	        		taskTy = "其它";
        	}
       
        }
    	return taskTy;
    }
    
    
    // 格式化时间
    commonUtils.formateDate =  function(value, row, index) {
        if(value==null||value == ""||value==undefined){
            return "";
        }
        var date = new Date(value);
        var month = date.getMonth()+1;
        var day = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        month = month<10?'0'+month:month;
        day = day<10?'0'+day:day;
        hours = hours<10?'0'+hours:hours;
        minutes = minutes<10?'0'+minutes:minutes;
        seconds = seconds<10?'0'+seconds:seconds;
        return date.getFullYear()+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
    }
    // 格式化日期
    commonUtils.formateDay =  function(value, row, index) {
        if(value==null||value == ""||value==undefined){
            return "";
        }
        var date = new Date(value);
        var month = date.getMonth()+1;
        var day = date.getDate();
        var date = new Date(value);
        month = month<10?'0'+month:month;
        day = day<10?'0'+day:day;
        return date.getFullYear()+"-"+month+"-"+day;
    }

    commonUtils.datePickerFormat = function(elemId) {
        laydate({
            elem : '#' + elemId,
            format : 'YYYY-MM-DD hh:mm:ss',
            istime : true,
            istoday : true
        });
    }
    /* paramObj示例 {isWeightS:$('#test1').val(),zoName:$('#zoName').val()} */
    /* 分页  tableName:table名称,paramObj:参数对象,params:table内部对象     */
    commonUtils.paginationCommon = function(tableName,paramObj,params){
        var result = {};
        result.limit = params.limit;
        result.page = $('#'+tableName).bootstrapTable('getOptions').pageNumber;

        if(typeof(paramObj) != undefined && typeOf(paramObj) == "object"){
            for(var param in paramObj){
                result.param = paramObj.param;
            }
        }
        return result;
    }

    /**
     * 获取可点击值
     */
    commonUtils.nameShow = function(value){
        return "<a href='javascript:void(0);'>"+value+"</a>";

    }



    // 格式化时间
    commonUtils.formateTimeStr = function formateDate(value, row, index) {
        if (value != null) {
            var vDate = new Date(value);
            return vDate.format("yyyy-MM-dd HH:mm:ss");
        } else {
            return "";
        }
    }

    // 格式化时间
    commonUtils.formateDateStr = function formateDate(value, row, index) {
        if (value != null) {
            var vDate = new Date(value);
            return vDate.format("yyyy-MM-dd");
        } else {
            return "";
        }
    }

    //jqgrid
    commonUtils.grid = (function(params) {
        var rowNum = 10;
        var define = {
            mtype:"post",
            datatype: "json",
            height: 350,
            viewrecords: true,
            total: true,
            rowNum: rowNum,
            pager: "#grid-pager",
            altRows: true,
            multiselect: true,
            multiboxonly: true,
            loadComplete: function () {
                var table = this;
                setTimeout(function () {
                    styleCheckbox(table);
                    updateActionIcons(table);
                    updatePagerIcons(table);
                }, 0);
            },
            autowidth: true,
            shrinkToFit: true,
            jsonReader: {
                root: "rows", page: "currentPage", total: "totalPages",
                records: "totalRecords", repeatitems: false, id: "id"
            },
            postData: {
                limit: rowNum
            }

        };
        //it causes some flicker when reloading or navigating grid
        //it may be possible to have some custom formatter to do this as the grid is being created to prevent this
        //or go back to default browser checkbox styles for the grid
        function styleCheckbox(table) {
        }
        //unlike navButtons icons, action icons in rows seem to be hard-coded
        //you can change them like this in here if you want
        function updateActionIcons(table) {
        }

        //replace icons with FontAwesome icons like above
        function updatePagerIcons(table) {
            var replacement =
            {
                'ui-icon-seek-first': 'icon-double-angle-left bigger-140',
                'ui-icon-seek-prev': 'icon-angle-left bigger-140',
                'ui-icon-seek-next': 'icon-angle-right bigger-140',
                'ui-icon-seek-end': 'icon-double-angle-right bigger-140'
            };
            $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
                var icon = $(this);
                var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
            })
        }

        //表格自适应浏览器窗口大小
        $(window).bind('resize', function () {
            var width = $('.col-xs-12').width();
            $('#grid-table').setGridWidth(width);
        });
        $.extend(define, params);
        $("#grid-table").jqGrid(define);
        $("#grid-table").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
    });

    //超时
    commonUtils.timeout=(function (param) {
        if($(param).val().length > 0){
            layer.alert("登录超时，请重新登录", {
                icon: 0,
                skin: 'layui-layer-lan'
                ,closeBtn: 0
            },function(){
                location.href="${basePath}/login";
            })
        }
    });
    //格式化jqGrid里面的序号
    commonUtils.formateID = function formateID (cellvalue, options, rowObject) {
        var rowListNum = parseInt($("#grid-table").jqGrid('getGridParam', 'rowNum'));
        var curpagenum = parseInt($("#grid-table").jqGrid('getGridParam', 'page'));
        var id = (rowListNum * (curpagenum - 1)) + parseInt(options.rowId);
        return parseInt(id);
    };

    // 格式化通知类型
    commonUtils.priority =  function(value, row, index) {
        if(value==null||value == ""||value==undefined){
            return "";
        }
        if(value==1){
            return "普通";
        }
        if(value==2){
            return "高";
        }
        if(value==3){
            return "紧急";
        }
        return "";
    }

    window.CommonUtils = commonUtils;

}(jQuery));